import bluetooth.Server;
import bluetooth.ServerListener;
import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Display;
import Logika.*;

/**
 * @author pmp
 */






public class ServerTest extends MIDlet implements ServerListener
{

    private Vector      clientAdres=new Vector();
    private Server      server;
    private String      now;
    private String      adress;
    private Display     display;
    private int         aktualnieObslugiwany;
    private MindServer  serverGry=new MindServer();
    MyGameCanvas        game=new MyGameCanvas();
    Pocisk              pocisk=new Pocisk();
    private int   whichPlayer=0; // indicate which player is connected to the server
    private int[] newTankPosition=new int[3];
    private Register register = new Register();

    public void startApp()
    {
        display=Display.getDisplay(this);
        display.setCurrent(game);
        server=new Server();
        server.createServer(this);
        server.publishServer();
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void clientConnected(String btAdress)
    {
        whichPlayer=register.add(btAdress);
        newTankPosition=serverGry.getNewTankPosition();
        System.out.println("---------------------------------------------------");
        System.out.println("wP="+ whichPlayer+"x= "+newTankPosition[0]+", y= "+newTankPosition[1]+" ,d= "+newTankPosition[2]);
        System.out.println("---------------------------------------------------");
        clientAdres.addElement(btAdress);
        serverGry.setTank(newTankPosition[0], newTankPosition[1], newTankPosition[2], whichPlayer , btAdress);// W tym miejscu mozna podac polozenia Tank, lub przeslac z klienta
        sendToAll(serverGry.getStoredRozlad()+(char)newTankPosition[0]+(char)newTankPosition[1]+(char)newTankPosition[2]+(char)whichPlayer);
    }

    public void connectionLost(String btAdress) {
        serverGry.remove(btAdress);
        register.remove(btAdress);
        sendToAll(serverGry.getStoredRozlad());
    }

        boolean status;
        Tank tank;

    public void dataReceived(String data, String btAdress) {
        String komunikat    ="";
        if(!serverGry.deadPlayers[clientAdres.indexOf(btAdress)]){
        if((int)data.charAt(0)==Zmienne.FIRE)
        {

            Tank tank= serverGry.getTank(btAdress);
            pocisk.setX(tank.zwrocPolozenieX());
            pocisk.setY(tank.zwrocPolozenieY());
            pocisk.setDirection(tank.zwrocKierunek());
            pocisk.increment();


            boolean status=true;
            while(serverGry.getBool(pocisk.getIncrementedX(), pocisk.getIncrementedY()))
            {
                sendToAll(serverGry.setRozkladForOpenSpace(pocisk.getIncrementedX(), pocisk.getIncrementedY() , pocisk.getPolozenieX() , pocisk.getPolozenieY(), tank.zwrocKierunek(), status));
                status=false;
                pocisk.increment();
                try
                {
                    Thread.sleep(110);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            if(serverGry.out_of_range(pocisk.getIncrementedX(), pocisk.getIncrementedY()))
            {
                sendToAll(serverGry.setRozkladForRange(pocisk.getPolozenieX(), pocisk.getPolozenieY(), status));
            }
            else
                if(serverGry.getDecision(pocisk.getIncrementedX(), pocisk.getIncrementedY()))
                {
                    sendToAll(serverGry.setRozkladForStop(pocisk.getIncrementedX(), pocisk.getIncrementedY() , pocisk.getPolozenieX() , pocisk.getPolozenieY(), status));
                }
                else
                {
                    sendToAll(serverGry.setRozkladForDestroyStop(pocisk.getIncrementedX(), pocisk.getIncrementedY() , pocisk.getPolozenieX() , pocisk.getPolozenieY(), status));
                }
        }
        else
        {
            if(!(komunikat=serverGry.move(data, btAdress)).equals(""))
            {
                sendToAll(komunikat);
            }
        }

    }
    }

    private void sendToAll(String komunikat)
    {
            for(int i=0; i<serverGry.size() ; i++)
            {
                server.sendData(komunikat,serverGry.elementAt(i));
            }
    }


    // Ta klasa jest niepotrzebna, pozniej bedzie prezentowany ekran z aktualna gra
    private class MyGameCanvas extends GameCanvas
    {
        public MyGameCanvas()
        {
            super(false);
            setFullScreenMode(true);
        }

        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(0x000000);
            g.fillRect(0, 0, 240, 320);
            g.setColor(0xFFFFFF);
            g.drawString("get and send= "+now, 30 , 30, 0);
        }
    }
}
